package com.wangxiaonan.wxwork.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.wxwork.domain.Agent;
import com.wangxiaonan.wxwork.exeption.ServiceException;
import com.wangxiaonan.wxwork.service.AgentService;
import com.wangxiaonan.wxwork.service.TokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用管理接口
 *
 * @Author: wangxiaonan
 * @Date: 2018/8/22
 **/
@RestController
@RequestMapping("/agent")
public class AgentController {
    private static final String AGENT_TOKEN_KEY = "agent_";

    @Resource
    private TokenService tokenService;
    @Resource
    private AgentService agentService;

    /**
     * 1. 绑定应用
     *
     * @param agent
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addAgent(@RequestBody Agent agent) {
        JSONObject rs = new JSONObject();
        String token;
        try {
            token = tokenService.getTokenString(agent.getCorpId(), agent.getSecret());
        } catch (ServiceException e) {
            rs.put("status", e.getCode());
            rs.put("msg", e.getMsg());
            return rs.toJSONString();
        }
        if (StringUtils.isBlank(token)) {
            rs.put("status", "fail");
            rs.put("msg", "not found agent fromt qywx");
            return rs.toJSONString();
        }
        int result = agentService.saveAgent(agent);
        if (result == 0) {
            rs.put("status", "fail");
            rs.put("msg", "insert database fail");
            return rs.toJSONString();
        }
        rs.put("status", "ok");
        return rs.toJSONString();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String agent(@PathVariable("id") String agentId) {
        Agent agent = agentService.queryAgentById(agentId);
        String token;
        JSONObject jsonAgent = new JSONObject();
        try {
            token = tokenService.getTokenString(agent.getCorpId(), agent.getSecret());
            jsonAgent = agentService.getAgent(token, agentId);
        } catch (ServiceException e) {

        }
        return jsonAgent.toJSONString();
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String agentList(@RequestParam(value = "limit_start", required = false) Integer limitStart,
                            @RequestParam(value = "page_size", required = false) Integer pageSize) {

        List<Agent> agents = agentService.queryAgentList(limitStart, pageSize);
        JSONObject rs = new JSONObject();
        rs.put("status", "ok");
        if (agents == null) {
            rs.put("data", new JSONArray());
            return rs.toJSONString();
        }
        String token;
        JSONArray jsonArray = new JSONArray();
        for (Agent agent : agents) {
            try {
                token = tokenService.getTokenString(agent.getCorpId(), agent.getSecret());
                JSONObject jsonAgent = agentService.getAgent(token, agent.getAgentId());
                jsonArray.add(jsonAgent);
            } catch (ServiceException e) {
                continue;
            }
        }
        rs.put("data", jsonArray);
        return rs.toJSONString();
    }
}
